package com.gitee.zhuhjay.xunfei.sdk;

import com.gitee.zhuhjay.xunfei.sdk.consts.ApiVersion;
import com.gitee.zhuhjay.xunfei.sdk.consts.ConfigConst;
import com.gitee.zhuhjay.xunfei.sdk.model.ApiAuth;
import com.gitee.zhuhjay.xunfei.sdk.util.XfUtil;
import okhttp3.OkHttpClient;

import java.util.concurrent.TimeUnit;

/**
 * 讯飞AI交互客户端工厂
 *
 * @author ZhuHJay
 * @since 2023/11/4 11:03
 */
public final class AppClientFactory {

    private static OkHttpClient okHttpClient;
    private static ApiAuth apiAuth;
    private static int clientV1_5Qps;
    private static int clientV2_0Qps;
    private static int clientV3_0Qps;
    private static int clientV3_5Qps;

    static {
        AppClientFactory.okHttpClient = new OkHttpClient.Builder()
                .connectTimeout(5, TimeUnit.SECONDS)
                .readTimeout(5, TimeUnit.SECONDS)
                .build();
    }

    private AppClientFactory(ApiAuth apiAuth, int qps1, int qps2, int qps3, int qps4) {
        apiAuth.check();
        XfUtil.checkBatchQps(new int[]{qps1, qps2, qps3, qps4});
        AppClientFactory.apiAuth = apiAuth;
        AppClientFactory.clientV1_5Qps = qps1;
        AppClientFactory.clientV2_0Qps = qps2;
        AppClientFactory.clientV3_0Qps = qps3;
        AppClientFactory.clientV3_5Qps = qps4;
    }

    /**
     * 创建客户端工厂, 默认并发数限制为 1
     *
     * @param apiAuth api授权信息
     * @return AppClientFactory
     */
    public static AppClientFactory create(ApiAuth apiAuth) {
        return new AppClientFactory(apiAuth,
                ConfigConst.API_DEFAULT_QPS, ConfigConst.API_DEFAULT_QPS,
                ConfigConst.API_DEFAULT_QPS, ConfigConst.API_DEFAULT_QPS);
    }

    /**
     * 创建客户端工厂, 可以自定义并发数限制
     *
     * @param apiAuth api授权信息
     * @param qps1    v1.5版本的并发数限制
     * @param qps2    v2.0版本的并发数限制
     * @param qps3    v3.0版本的并发数限制
     * @param qps4    v3.5版本的并发数限制
     * @return AppClientFactory
     */
    public static AppClientFactory create(ApiAuth apiAuth, int qps1, int qps2, int qps3, int qps4) {
        return new AppClientFactory(apiAuth, qps1, qps2, qps3, qps4);
    }

    /**
     * 获取 v1.5 版本的客户端
     *
     * @return AppClient
     */
    public AppClient openClientV1() {
        return AppClientV1Holder.CLIENT_V1;
    }

    private static class AppClientV1Holder {
        private static final AppClient CLIENT_V1 =
                new AppClient(apiAuth, ApiVersion.V1_5, okHttpClient, clientV1_5Qps);
    }

    /**
     * 获取 v2.0 版本的客户端
     *
     * @return AppClient
     */
    public AppClient openClientV2() {
        return AppClientV2Holder.CLIENT_V2;
    }

    private static class AppClientV2Holder {
        private static final AppClient CLIENT_V2 =
                new AppClient(apiAuth, ApiVersion.V2_0, okHttpClient, clientV2_0Qps);
    }

    /**
     * 获取 v3.0 版本的客户端
     *
     * @return AppClient
     */
    public AppClient openClientV3() {
        return AppClientV3Holder.CLIENT_V3;
    }

    private static class AppClientV3Holder {
        private static final AppClient CLIENT_V3 =
                new AppClient(apiAuth, ApiVersion.V3_0, okHttpClient, clientV3_0Qps);
    }

    /**
     * 获取 v3.5 版本的客户端
     *
     * @return AppClient
     */
    public AppClient openClientV3_5() {
        return AppClientV3_5Holder.CLIENT_V3_5;
    }

    private static class AppClientV3_5Holder {
        private static final AppClient CLIENT_V3_5 =
                new AppClient(apiAuth, ApiVersion.V3_5, okHttpClient, clientV3_5Qps);
    }

}
